package com.college.controllers;

import java.util.List;

import javax.jms.Session;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.college.domain.Department;
import com.college.domain.Faculty;
import com.college.domain.Roles;
import com.college.domain.SearchFaculity;
import com.college.domain.Users;
import com.college.interfaces.IConfigurationDAO;
import com.college.interfaces.IFacultyDAO;
import com.college.util.Encryption;

@Controller("/faculty")
public class FacultyController {
	
	@Autowired
	private IConfigurationDAO configDAO;
	
	@Autowired
	private IFacultyDAO facultyDAO;

	@RequestMapping(value="/addFaculty", method=RequestMethod.GET)
	public ModelAndView getFacultyLoginInfoForm()
	{
		ModelAndView mav = new ModelAndView("login_details");
		Users user = new Users();
		mav.getModelMap().put("login_details", user);
		return mav;
	}
	@RequestMapping(value="/addFaculty", method=RequestMethod.POST)
	public String saveCollege(@ModelAttribute("login_details")@Valid Users user, BindingResult result, SessionStatus status, ModelMap model, HttpSession session){
		user.setPassword(Encryption.encrypt(user.getPassword()));
		if(result.hasErrors()){
			return "login_details";
		}
		//Integer res = configDAO.saveCollege(college);
		/*if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "College is added Successfully.");*/
			session.setAttribute("userDetails", user);
		//}
		return "redirect:facultyInfo";
		
	}
	
	@RequestMapping(value="/facultyInfo", method=RequestMethod.GET)
	public ModelAndView getFacultyInfoForm()
	{
		ModelAndView mav = new ModelAndView("faculty_info");
		Faculty faculty = new Faculty();
		mav.getModelMap().put("faculty_info", faculty);
		return mav;
	}
	
	@RequestMapping(value="/facultyInfo", method=RequestMethod.POST)
	public String saveCollege(@ModelAttribute("faculty_info")@Valid Faculty faculty, BindingResult result, SessionStatus status, ModelMap model, HttpSession session){
		if(result.hasErrors()){
			return "faculty_info";
		}
		//Integer res = configDAO.saveCollege(college);
		/*if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "College is added Successfully.");*/
			session.setAttribute("facultyInfo", faculty);
		//}
		return "redirect:roleInfo";
		
	}
	
	@RequestMapping(value="/roleInfo", method=RequestMethod.GET)
	public ModelAndView getFacultyRoleInfoForm()
	{
		ModelAndView mav = new ModelAndView("role_Info");
		Roles role = new Roles();
		mav.getModelMap().put("role_Info", role);
		return mav;
	}
	
	@RequestMapping(value="/roleInfo", method=RequestMethod.POST)
	public String saveCollege(@ModelAttribute("role_Info")@Valid Roles role, BindingResult result, SessionStatus status, ModelMap model, HttpSession session){
		if(result.hasErrors()){
			return "role_Info";
		}
		//Integer res = configDAO.saveCollege(college);
		/*if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "College is added Successfully.");*/
			session.setAttribute("roleInfo", role);
		//}
		return "redirect:deptInfo";
		
	}
	
	@RequestMapping(value="/deptInfo", method=RequestMethod.GET)
	public ModelAndView getFacultyDeptInfoForm(ModelMap model)
	{
		ModelAndView mav = new ModelAndView("dept_Info");
		List<String> collegeList = configDAO.getCollegeListForDropDown();
		model.addAttribute("collegelist",collegeList);
		Department dept = new Department();
		mav.getModelMap().put("dept_Info", dept);
		return mav;
	}
	
	@RequestMapping(value="/deptInfo", method=RequestMethod.POST)
	public String saveCollege(@ModelAttribute("dept_Info") Department dept, BindingResult result, SessionStatus status, ModelMap model, HttpSession session){
		if(result.hasErrors()){
			return "dept_Info";
		}
		//Integer res = configDAO.saveCollege(college);
		/*if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "College is added Successfully.");*/
			session.setAttribute("deptInfo", dept);
			Users user = (Users) session.getAttribute("userDetails");
			Faculty faculty = (Faculty) session.getAttribute("facultyInfo");
			Roles role = (Roles) session.getAttribute("roleInfo");
			Department depart = (Department) session.getAttribute("deptInfo");
			facultyDAO.saveFacultyInfo(user, faculty, role, dept);
			model.addAttribute("savedMsg","Faculty added Successfully");
		//}
		return "dept_Info";
		
	}
	
	@RequestMapping(value="/searchFaculity", method=RequestMethod.GET)
	public ModelAndView getSearchFaculityForm(ModelMap model)
	{
		ModelAndView mav = new ModelAndView("search_faculity");
		SearchFaculity searchFaculity = new SearchFaculity();
		mav.getModelMap().put("search_faculity", searchFaculity);
		return mav;
	}
	
	@RequestMapping(value="/searchFaculity", method=RequestMethod.POST)
	public String searchFaculty(@ModelAttribute("search_faculity") SearchFaculity searchKey, BindingResult result, SessionStatus status, ModelMap model, HttpSession session){
		if(result.hasErrors()){
			return "search_faculity";
		}
			
			PagedListHolder<Faculty> facultyList = new PagedListHolder<Faculty>();
			List<Faculty> facList = facultyDAO.getAllFAcultyByNameOrId(searchKey.getSearchKey());
			if(facList.size()==0){
				model.addAttribute("savedMsg", "No Faculty present with this Search Key.");
			}
			/*facultyList =  facultyDAO.getAllFAcultyByNameOrId(searchKey.getSearchKey());
			facultyList.setPageSize(4);*/
			session.setAttribute("listItems", facultyList);
			model.addAttribute("facultyList",facList);
		return "search_faculity";
		
	}
}
